package com.xjrsoft.module.oa.controller;


import com.xjrsoft.common.result.Response;
import com.xjrsoft.module.base.controller.XjrBaseAnnexesFileController;
import com.xjrsoft.module.oa.dto.FileDto;
import com.xjrsoft.module.oa.dto.FilefolderDto;
import com.xjrsoft.module.oa.service.IXjrOafilemanageService;
import com.xjrsoft.module.upload.controller.UploadController;
import com.xjrsoft.module.upload.vo.UploadFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author zwq
 * @since 2021-04-20
 *  @description:对文件和文件夹管理的Controller
 */
@RestController
@AllArgsConstructor
@RequestMapping("/file-manager")
@Api(value = "/file-manager",tags = "文件管理")
public class XjrOafilemanageController {

    @Autowired
    private IXjrOafilemanageService xjrOafilemanageService;

    @Autowired
    private UploadController uploadController;

    @Autowired
    private XjrBaseAnnexesFileController xjrBaseAnnexesFileController;




    /**
     * @author 湘北智造-框架开发组
     * @date:2019年12月16日
     * @url: /queryFileOrFileFolder
     * @description:查询文件 fileType: 0-所有文件 1-文档 2-图片 3-回收站 4-我的共享 5-他人共享,
     */
    @GetMapping(value = "/queryFileAndFileFolder")
    @ApiOperation(value="查询文件")
    public Response queryFileAndFileFolder(FileDto fileDto) throws Exception {
        return Response.ok(xjrOafilemanageService.queryFileAndFolder(fileDto));
    }



    @PostMapping(value = "/uploadFile")
    @ApiOperation(value="上传文件")
    public Response uploadFile(@RequestParam("file") MultipartFile multfile,@RequestParam(value = "folderId",required=false) String folderId) throws Exception {
        //上传文件
        Response<UploadFileVo> uploadFileVoResponse = uploadController.uploadFile(null, multfile);
        UploadFileVo uploadFileVo = uploadFileVoResponse.getData();
        return Response.ok(xjrOafilemanageService.addFileInfo(uploadFileVo,multfile,folderId));
    }



    @GetMapping(value = "/downloadFile/{fileId}")
    @ApiOperation(value="下载文件")
    public Response downloadFile(@PathVariable String fileId) throws IOException {
        xjrBaseAnnexesFileController.getFileById(fileId);
        return Response.ok();
    }


    @DeleteMapping(value = "/logicDelete/{fileId}")
    @ApiOperation(value="文件/文件夹删除（放入回收站中)")
    public Response logicDelete(@PathVariable String fileId) throws Exception {
        return Response.status(xjrOafilemanageService.logicDelete(fileId));
    }

    
    @DeleteMapping(value = "/Delete/{fileId}")
    @ApiOperation(value="彻底删除(回收站中彻底删除)")
    public Response delete(@PathVariable String fileId) throws Exception {
        return Response.status(xjrOafilemanageService.delete(fileId));
    }


    /**
     * @author 湘北智造-框架开发组
     * @date:2020年7月1日
     * @url: /reviewFile
     * @description:在线预览文件
     * operation:1-预览
     */
    @GetMapping(value = "/reviewFile/{fileId}")
    @ApiOperation(value="在线预览文件")
    public Response reviewFile(@PathVariable String fileId) throws Exception {
        return Response.ok(xjrOafilemanageService.reviewOrEditFile(fileId,"1"));
    }

    /**
     * @author 湘北智造-框架开发组
     * @date:2020年7月1日
     * @url: /reviewFile
     * @description:在线编辑文件
     * operation:2-编辑
     */
    @GetMapping(value = "/EditFile/{fileId}")
    @ApiOperation(value="在线编辑文件")
    public Response editFile(@PathVariable String fileId) throws Exception {
        return Response.ok(xjrOafilemanageService.reviewOrEditFile(fileId,"2"));
    }



 
    @PutMapping("/shareFileOrFileFolder")
    @ApiOperation(value="共享文件")
    public Response shareFileOrFileFolder(@RequestParam("fileId") String fileId) {
        return Response.status(xjrOafilemanageService.shareFileOrFileFolder(fileId));
    }


    @GetMapping(value = "/getFileByParentId/{parentId}")
    @ApiOperation(value="查询文件下的文件夹和文件")
    public Response getFileByParentId(@PathVariable String parentId) {
        return Response.ok(xjrOafilemanageService.getFolderInfoByParentId(parentId));
    }



    @PutMapping(value = "/restoreFile/{fileId}")
    @ApiOperation(value="回收站中还原文件")
    public Response restoreFile(@PathVariable String fileId) {
        return Response.status(xjrOafilemanageService.restoreFile(fileId));
    }



    @DeleteMapping(value = "/emptyTrash")
    @ApiOperation(value="清空回收站")
    public Response emptyTrash() throws Exception {
        return Response.status(xjrOafilemanageService.emptyTrash());
    }

 
    @PostMapping(value = "/addFolder")
    @ApiOperation(value="新建文件夹")
    public Response addFolder(@RequestBody FilefolderDto filefolderDto) throws Exception {
        return Response.status(xjrOafilemanageService.addFolder(filefolderDto));
    }

 
    @PutMapping(value = "/updateFileName")
    @ApiOperation(value="文件/文件夹重命名")
    public Response updateFileName(@RequestParam("fileId") String fileId,@RequestParam("fileName") String fileName) throws Exception {
        return Response.status(xjrOafilemanageService.updateFileName(fileId,fileName));
    }

 
    @PutMapping(value = "/updateFile")
    @ApiOperation(value="更新文件内容")
    public Response updateFile(@RequestParam("fileId") String fileId,@RequestParam("file") MultipartFile multfile) throws Exception {
        return Response.status(xjrOafilemanageService.updateFile(fileId,multfile));
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value="获取所有文件夹树")
    public Response getFileFolderTree() throws Exception {
        return Response.ok(xjrOafilemanageService.getFileFolderTree());
    }


    @PutMapping(value = "/removeFile")
    @ApiOperation(value="移动文件或文件夹")
    public Response removeFile(@RequestParam("fileId") String fileId,@RequestParam("folderId") String folderId) throws Exception {
        return Response.status(xjrOafilemanageService.removeFile(fileId,folderId));
    }

}
